package com.example.fengyungo_user.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.example.fengyungo_user.R;
import com.example.fengyungo_user.fragment.MainFragment;
import com.example.fengyungo_user.fragment.MeFragment;
import com.example.fengyungo_user.fragment.MessageFragment;

public class ViewPagerAdapter extends FragmentPagerAdapter {

    private Context mContext;

    //下面导航栏的名字
    private String[] name = {"主页","消息","我的"};

    //导航栏对应的图标
    private int[] icon = {R.drawable.main_grey,R.drawable.message_grey,R.drawable.me_grey};

    //适配器的参数，Context和FragmentManager
    public ViewPagerAdapter(Context mContext, FragmentManager fm) {
        super(fm);
        this.mContext = mContext;
    }

    //创建导航栏碎片
    @NonNull
    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch (position) {
            case 0:
                fragment = new MainFragment();
                break;
            case 1:
                fragment = new MessageFragment();
                break;
            case 2:
                fragment = new MeFragment();
                break;
            default:
                break;
        }
        return fragment;
    }

    //获取名字长度，name是自己创建的字符数组
    //获取字符串个数
    @Override
    public int getCount() {
        return name.length;
    }

    //获取名字的位置，这里只有三个，所以是0,1,2
    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return name[position];
    }

    //对tablayout的两个控件进行处理，进行初始化工作
    public View getTabView(int position) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.tablayout,null);
        ImageView Icon = v.findViewById(R.id.TabIcon);
        TextView Text = v.findViewById(R.id.TabText);
        Icon.setImageResource(icon[position]);//设置图片
        Text.setText(name[position]);//设置文字
        //初始情况就是第一个会默认变色，所以要单独处理
        if (position==0) {
            Icon.setImageResource(R.drawable.main_blue);
            Text.setTextColor(v.getResources().getColor(R.color.blue));
        }
        return v;
    }
}

